package hibernateDAOS;

import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.Mozo;

public class MozoDAO {
	
	private static MozoDAO instancia = null;
	private static SessionFactory sf = null;

	public static MozoDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new MozoDAO();
		} 
		return instancia;
	}
	
	public void guardarMozo(Mozo m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarMozo(Mozo m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarMozo(Mozo m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<Mozo> obtenerMozos(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Mozo");
		List<Mozo> mozos = q.list();
		session.close();
		return mozos;
	}

	@SuppressWarnings("unchecked")
	public Mozo obtenerMozo(Mozo m){
		Session session = sf.openSession();
		session.load(m, m.getIdMozo());
		session.close();
		return m;
	}
	
	@SuppressWarnings("unchecked")
	public Mozo obtenerMozoId(int id){
		Mozo m = new Mozo();
		m.setIdMozo(id);
		
		Session session = sf.openSession();
		session.load(m, m.getIdMozo());
		session.close();
		return m;
	}
}



